package com.czy.owner.ui.workorder;

import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;

import com.czy.owner.R;
import com.czy.owner.adapter.PickCouponAdapter;
import com.czy.owner.entity.PrePayBalancePaymentModel;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.PhoneUtils;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by King on 2016/11/29 0029.
 */
public class PickCouponActivity extends BaseActivity {
    @BindView(R.id.elv_coupon)
     ExpandableListView elvCoupon;
    private PrePayBalancePaymentModel data = null;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_pick_coupon;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        if (getIntent().getExtras() == null || (data = (PrePayBalancePaymentModel) getIntent().getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY)) == null) {
            PhoneUtils.ShowToastMessage(PickCouponActivity.this, "参数异常!");
            finish();
            return;
        }
        initTitle();
        initView();
    }

    @Override
    protected void updateViews() {

    }


    private void initTitle() {
        getToolbarTitle().setText("使用优惠券");
        getSubTitle().setText("确定");
    }

    @OnClick(R.id.toolbar_subtitle)
    public void sure(View view) {
        int pickedCount = 0;
        ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i < data.getCouponsList().size(); i++) {
            if (data.getCouponsList().get(i).isPicked() && !data.getCouponsList().get(i).getCouponsType().equals("serviceVoucher")) {
                pickedCount++;
                result.add("" + data.getCouponsList().get(i).getUserWalletId());
            }
        }
        Intent intent = new Intent();
        intent.putStringArrayListExtra(Constants.TRANSMISSION_CONTENT_KEY, result);
        setResult(RESULT_OK, intent);
        finish();
    }


    private void initView() {
        if (data.getCouponsList().size() != 0) {
//            PickCouponAdapter mAdapter = new PickCouponAdapter(this,data.getCouponsList(),data.isAllCoupons(),data.getTailAmount());
            PickCouponAdapter mAdapter = new PickCouponAdapter(this, data.getCouponsList(), false, data.getTailAmount());
            elvCoupon.setAdapter(mAdapter);
            for (int i = 0; i < mAdapter.getGroupCount(); i++) {
                elvCoupon.expandGroup(i);
            }
            elvCoupon.setGroupIndicator(null);
            elvCoupon.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                    return true;
                }
            });
        }
    }


}
